//加载express模块
const express = require('express');
//启用服务器
const app = express();
//创建端口
app.listen(3000);
const cors = require("cors");
app.use(cors({
	origin: ['http://localhost:8080', 'http://localhost:3000'],
	credentials: true
}))
//引入连接池模块
const pool = require('./pool.js')
//引入body-parser
const bodyParser = require('body-parser');
//加载路由器Router
const userRouter = require('./Router/user.js');
const shopRouter = require('./Router/shopping.js');
const e = require('express');
//托管到静态页面
// app.use(express.static('./public'))

//应用body-parser中间件将流的方式请求的数据解析为对象
app.use(bodyParser.urlencoded({
	extended: false
}));

//错误处理中间件
//要拦截产生的错误
app.use((err, req, res, next) => {
	//err接收的错误
	console.log(err);
	res.send({
		code: 500,
		msg: "服务器端错误"
	});
});

app.post('/login', (req, res, next) => {
	let obj = req.body;
	console.log(obj);
	//执行sql代码
	pool.query('select * from user where phone=? and password=?', [obj.phone, obj.password], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		console.log(result);
		if (result.length === 0) {
			res.send({
				code: 303,
				msg: "您输入的用户名或密码错误"
			})
		} else {
			res.send({
				code: 300,
				msg: "登录成功",
				data:result
			})
		}

	})
});
//使用post的方法获得注册数据
app.post('/reg', (req, res, next) => {
	//获取流数据
	let obj = req.body;
	// console.log(obj);
	//执行SQL命令
	console.log(obj.phone,obj.password);
	pool.query('select * from user where phone=?', [obj.phone], (err, result) => {
		console.log(result.length==0);
		// console.log(result[0].phone);
		// console.log(obj.phone);
		if (err) {
			next(err);
			return;
		}
		if (result.length!==0) {
			res.send({
				code: 303,
				msg: '该手机号已注册'
			})
		} else {
			pool.query('insert into user set ?', [obj], (err, result) => {
				if (err) {
					next(err);
					return;
				};
				// console.log(result);
				res.send({
					code: 200,
					msg: '注册成功'
				})
			})
		}
	});
})

app.get('/shoucang', (req, res) => {
	// SQL语句以获取scy表的数据
	let sql = 'select * from scy';
	// 执行SQL语句
	pool.query(sql, (error, results) => {
	  if (error) throw error;
	  console.log(results)
	  res.send({ message: 'ok', code: 200, results: results });
	});
  });
 app.get('/', (req, res) => {
	// SQL语句以获取scy表的数据
	let sql = 'select * from nr';
	// 执行SQL语句
	pool.query(sql, (error, results) => {
	  if (error) throw error;
	  console.log(results)
	  res.send({ message: 'ok', code: 200, results: results });
	});
  });

//使用get方法获取首页数据
app.get('/', (req, res, next) => {
	let obj = req.query;
	console.log(obj)
	//计算开始查询的值
	let start = (obj.pno - 1) * obj.count;
	//将每页数据量转为整型
	let size = parseInt(obj.count);
	//执行SQL命令
	pool.query('select * from detail limit ?,?', [start, size], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		console.log(result);
		res.send({
			code: 200,
			msg: '查询成功',
			msg: '查询成功',
			data: result
		})
	})
})


//使用get方法获取详情页数据
app.get('/detail', (req, res, next) => {
	let obj = req.query;
	console.log(obj)
	//将每页数据量转为整型
	let id = parseInt(obj.id);
	//执行SQL命令
	pool.query('select * from detail where id=?', [id], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		console.log(result);
		res.send({
			code: 200,
			msg: '查询成功',
			msg: '查询成功',
			data: result
		})
	})
})
app.get('/photo', (req, res, next) => {
	let obj = req.query;
	console.log(obj)
	//将每页数据量转为整型
	let id = parseInt(obj.id);
	//执行SQL命令
	pool.query('select photo from photo where detail_id=?', [id], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		console.log(result);
		res.send({
			code: 200,
			msg: '查询成功',
			msg: '查询成功',
			data: result
		})
	})
})


